package Version1;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BootStrap {

    private int port=8080;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void startUp() throws IOException {

        //创建一个服务器Socket对象
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("=====>>>Minicat start on port：" + port);

        while(true){

            //开始这里就开始阻塞接收了
            //浏览器发起一个TCP/IP请求，服务端就开始阻塞监听了
            Socket socket = serverSocket.accept();

            System.out.println("接收到了请求...");
            //获得输出流对象
            OutputStream outputStream = socket.getOutputStream();
            String data="Hello minicat";

            //返回给浏览器时，必须要有特定的格式头，才能被浏览器识别
            String responseText= HttpProtocolUtil.getHttpHeader200(data.getBytes().length)+data;
            outputStream.write(responseText.getBytes());
            socket.close();
        }
    }

    public static void main(String[] args) {
        BootStrap bootStrap = new BootStrap();

        try {
            bootStrap.startUp();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
